﻿using B.S.Data.MES.API.Applications.QualityManagement.ExamineTypeModel.Command;
using B.S.Domain.BasicData.QualityManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.QualityManagement.ExamineTypeModel.CommandHandler
{
    public class AllDeleteExamineTypeCommandHandler : IRequestHandler<AllDeleteExamineTypeCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<ExamineType> _examinetypeRepository;
        public AllDeleteExamineTypeCommandHandler(IBaseRepository<ExamineType> examinetypeRepository)
        {
            _examinetypeRepository = examinetypeRepository;
        }
        public async Task<APIRequst<int>> Handle(AllDeleteExamineTypeCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> result = new APIRequst<int>();

            var idlist = request.textArray.Split(',').Select(x => Convert.ToInt32(x)).ToList();

            if(idlist==null)
            {
                result.Code = RequstCode.失败;
                result.Msg = RequstCode.失败.ToString();
                return await Task.FromResult(result);
            }
            var listment = _examinetypeRepository.GetAll().Where(x => idlist.Contains(x.ExamineId)).ToList();
            foreach (var item in listment)
            {
                item.isDel = true;
            }
            var code = await _examinetypeRepository.UpdateRange(listment);
            if(code>0)
            {
                result.Code = RequstCode.成功;
                result.Msg = RequstCode.成功.ToString();
               
            }
            return await Task.FromResult(result);

        }
    }
}
